package com.etang.a2020b1006.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CompoundButton;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bizideal.smarthome.socket.ConstantUtil;
import com.bizideal.smarthome.socket.ControlUtils;
import com.bizideal.smarthome.socket.DataCallback;
import com.bizideal.smarthome.socket.DeviceBean;
import com.bizideal.smarthome.socket.SocketClient;
import com.etang.a2020b1006.R;
import com.etang.a2020b1006.tools.DiyToast;

import java.util.ArrayList;
import java.util.List;

public class BaseFragment extends Fragment {

    private TextView tv_hum, tv_press, tv_gas, tv_smo, tv_ill, tv_per, tv_pm, tv_co;
    public static float temp, hum, press, gas, smo, ill, per, pm, co;
    private ToggleButton tg_lamp, tg_warm, tg_fan, tg_door, tg_cur, tg_tv, tg_dvd, tg_kt;
    private List<String> list = new ArrayList<String>();
    private ListAdapter adapter;
    private ListView lv_state_link;
    private SharedPreferences sharedPreferences;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_base, null, false);
        initView(view);
        ControlUtils.getData();
        SocketClient.getInstance().getData(new DataCallback<DeviceBean>() {
            @Override
            public void onResult(final DeviceBean getdata) {
                if (!TextUtils.isEmpty(getdata.getAirPressure())) {
                    tv_press.setText(getdata.getAirPressure());
                    press = Float.valueOf(getdata.getAirPressure());
                }
                if (!TextUtils.isEmpty(getdata.getCo2())) {
                    tv_co.setText(getdata.getCo2());
                    co = Float.valueOf(getdata.getCo2());
                }
                if (!TextUtils.isEmpty(getdata.getGas())) {
                    tv_gas.setText(getdata.getGas());
                    gas = Float.valueOf(getdata.getGas());
                }
                if (!TextUtils.isEmpty(getdata.getHumidity())) {
                    tv_hum.setText(getdata.getHumidity());
                    hum = Float.valueOf(getdata.getHumidity());
                }
                if (!TextUtils.isEmpty(getdata.getIllumination())) {
                    tv_ill.setText(getdata.getIllumination());
                    ill = Float.valueOf(getdata.getIllumination());
                }
                if (!TextUtils.isEmpty(getdata.getSmoke())) {
                    tv_smo.setText(getdata.getSmoke());
                    smo = Float.valueOf(getdata.getSmoke());
                }
                if (!TextUtils.isEmpty(getdata.getPM25())) {
                    tv_pm.setText(getdata.getPM25());
                    pm = Float.valueOf(getdata.getPM25());
                }
                if (!TextUtils.isEmpty(getdata.getTemperature())) {
                    temp = Float.valueOf(getdata.getTemperature());
                }
                if (!TextUtils.isEmpty(getdata.getStateHumanInfrared())) {
                    per = Float.valueOf(getdata.getAirPressure());
                    if (per == 1) {
                        tv_per.setText("有人");
                    } else {
                        tv_per.setText("无人");
                    }
                }
                Log.e("TAG", "onResult: " + getdata.getWarningLight());
            }
        });
        tg_warm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.WarningLight, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_lamp.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.Lamp, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Lamp, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_fan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Fan, ConstantUtil.CHANNEL_ALL, ConstantUtil.CLOSE);
                }
            }
        });
        tg_door.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.RFID_Open_Door, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
                    CountDownTimer timer = new CountDownTimer(3000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            DiyToast.showToast(getActivity(), millisUntilFinished / 1000 % 60 + "秒后关闭门禁开关");
                        }

                        @Override
                        public void onFinish() {
                            tg_door.setChecked(false);
                        }
                    }.start();
                }
            }
        });
        tg_tv.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "1", ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "1", ConstantUtil.OPEN);
                }
            }
        });
        tg_kt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "2", ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "2", ConstantUtil.OPEN);
                }
            }
        });
        tg_dvd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "3", ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.INFRARED_1_SERVE, "3", ConstantUtil.OPEN);
                }
            }
        });
        tg_cur.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    ControlUtils.control(ConstantUtil.Curtain, ConstantUtil.CHANNEL_1, ConstantUtil.OPEN);
                } else {
                    ControlUtils.control(ConstantUtil.Curtain, ConstantUtil.CHANNEL_2, ConstantUtil.CLOSE);
                }
            }
        });
        handler.post(timeRunna);
        return view;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            list.clear();
            adapter = null;
            String smo_state = sharedPreferences.getString("smo_state", null);
            list.add("烟雾模式：" + smo_state);
            String temp_state = sharedPreferences.getString("temp_state", null);
            list.add("温度模式：" + temp_state);
            String ill_state = sharedPreferences.getString("ill_state", null);
            list.add("光照模式：" + ill_state);
            String per_state = sharedPreferences.getString("per_state", null);
            list.add("绿色模式：" + per_state);
            String pm_state = sharedPreferences.getString("pm_state", null);
            list.add("PM2.5模式：" + pm_state);
            adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
            lv_state_link.setAdapter(adapter);
            handler.postDelayed(timeRunna, 1000);
        }
    };
    Runnable timeRunna = new Runnable() {
        @Override
        public void run() {
            Message msg = handler.obtainMessage();
            handler.sendMessage(msg);
        }
    };

    private void initView(View view) {
        sharedPreferences = getActivity().getSharedPreferences("rember", Context.MODE_PRIVATE);
        lv_state_link = (ListView) view.findViewById(R.id.state_link);
        tg_cur = (ToggleButton) view.findViewById(R.id.tg_cur);
        tg_door = (ToggleButton) view.findViewById(R.id.tg_door);
        tg_dvd = (ToggleButton) view.findViewById(R.id.tg_dvd);
        tg_fan = (ToggleButton) view.findViewById(R.id.tg_fan);
        tg_kt = (ToggleButton) view.findViewById(R.id.tg_kt);
        tg_lamp = (ToggleButton) view.findViewById(R.id.tg_lamp);
        tg_tv = (ToggleButton) view.findViewById(R.id.tg_tv);
        tg_warm = (ToggleButton) view.findViewById(R.id.tg_warm);
        tv_co = (TextView) view.findViewById(R.id.tv_co);
        tv_gas = (TextView) view.findViewById(R.id.tv_gas);
        tv_hum = (TextView) view.findViewById(R.id.tv_hum);
        tv_ill = (TextView) view.findViewById(R.id.tv_ill);
        tv_per = (TextView) view.findViewById(R.id.tv_per);
        tv_pm = (TextView) view.findViewById(R.id.tv_pm);
        tv_press = (TextView) view.findViewById(R.id.tv_press);
        tv_smo = (TextView) view.findViewById(R.id.tv_smo);
    }
}
